package cn.new18.testactivitytask.base;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.AbsCallback;

import org.json.JSONObject;

import java.lang.reflect.Type;

import cn.new18.testactivitytask.R;
import okhttp3.Call;
import okhttp3.Response;
import com.google.gson.Gson;

/**
 * Created by RaoWei on 2016/11/28 16:01.
 */

public abstract class LoadingPage extends FrameLayout {
    public LoadingPage(Context context) {
        this(context, null);
    }

    public LoadingPage(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingPage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLoadingPage(context);
    }

    //1.定义3中页面状态
    public enum PageState {
        STATE_ERROR,
        STATE_SUCCESS,
        STATE_LOADING
    }
    //2.默认页面状态
    public PageState mState = PageState.STATE_LOADING;
    //3.定义3中视图
    private View loadingView;
    private View errorView;
    private View successView;

    //4.初始化page
    private void initLoadingPage(Context context) {
        //1.添加loadView
        if (loadingView == null) {
            loadingView = LayoutInflater.from(getContext()).inflate(R.layout.loading_view, null);
        }
        addView(loadingView);
        //2.添加successView
        if (successView == null) {
            successView = creatSuccessView();
        }
        if (successView != null) {
            addView(successView);
        } else {
            throw new IllegalArgumentException("the creatSuccessView() can not return null");
        }
        //3.添加errorView
        if (errorView == null) {
            errorView = LayoutInflater.from(getContext()).inflate(R.layout.error_view, null);
            View img = errorView.findViewById(R.id.error_img);

            img.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    onErrorLoadAgain();
                }
            });
        }
        addView(errorView);
        //4.显示默认view
        showPage();
        //5.加载数据
        //loadData(context);
    }


    private void loadData(final Context context) {
        OkGo.get("").execute(new AbsCallback<Object>() {
            @Override
            public void onSuccess(Object o, Call call, Response response) {

            }

            @Override
            public Object convertSuccess(Response response) throws Exception {
                return null;
            }
        });
    }

    private PageState checkData(Object data) {
        return null;
    }


    public void showPage() {
        //1.先隐藏所有view
        errorView.setVisibility(GONE);
        successView.setVisibility(GONE);
        loadingView.setVisibility(GONE);
        //2.根据状态显示相应view
        switch (mState) {
            case STATE_ERROR:
                errorView.setVisibility(VISIBLE);
                break;
            case STATE_SUCCESS:
                successView.setVisibility(VISIBLE);
                break;
            case STATE_LOADING:
                loadingView.setVisibility(VISIBLE);
                break;
        }
    }


    protected abstract void onErrorLoadAgain();
    protected abstract View creatSuccessView() ;
    //protected abstract Object onLoad();
}
